﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using OpenNETCF.Security.Cryptography;
using System.Security.Cryptography;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using FFWinMobile.Properties;

namespace FFWinMobile
{
    public partial class frmSubscribeAndUnsubscribe : Form
    {
        public enum Subscription
        {
            Subscribe,
           Unsubscribe
        }
        Subscription subsWork;
        string feedId, listID;
        public frmSubscribeAndUnsubscribe()
        {
            InitializeComponent();
        }
        public frmSubscribeAndUnsubscribe(Subscription sb, string feed_id, string list_id)
        {
            InitializeComponent();
            subsWork = sb;
            feedId = feed_id;
            listID = list_id;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {

        }

        private void frmSubscribeAndUnsubscribe_Load(object sender, EventArgs e)
        {
            pnlFeeds.Height = (this.Height - 41);
            if (GF.Giris)
            {
                GenerateUserPrompts();
            }
            else
            {
                frmLogin frm = new frmLogin();
                DialogResult dr = frm.ShowDialog();
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
                if (dr == DialogResult.OK)
                {
                    this.Show();
                    GenerateUserPrompts();
                }
                else
                {
                    Application.Exit();
                }
            }
           
        }

        private void GenerateUserPrompts()
        {
            int x = 10;
                int y = 10;
            if (subsWork == Subscription.Subscribe)
            {
                
                string user = FF.ReadUserFeed(feedId,0);
                JObject j = JObject.Parse(user);
                string userName = j["name"].Value<string>();
                Label lblUser = new Label();
                lblUser.Text = string.Format(Properties.Resources.SUBSCRIBE_PROMPT, userName);
                lblUser.Height = CFMeasureString.MeasureString(lblUser, lblUser.Text, pnlFeeds.ClientRectangle).Height;
                lblUser.Width = (pnlFeeds.ClientRectangle.Width - 20);
                lblUser.Location = new Point(x, y);
                y += (lblUser.Height + 3);

                pnlFeeds.Controls.Add(lblUser);

                string lists = FF.FeedUserLists();
                JObject l = JObject.Parse(lists);
                foreach (JToken list in l["sections"][0]["feeds"])
                {
                    CheckBox cb = new CheckBox();                    
                    cb.Tag = list["id"].Value<string>();
                    cb.Text = list["name"].Value<string>();
                    cb.Width = (pnlFeeds.ClientRectangle.Width-20);
                    cb.Height = CFMeasureString.MeasureString(cb, cb.Text, pnlFeeds.ClientRectangle).Height;
                    cb.Location = new Point(x, y);
                    y += (cb.Height + 3);
                    if (cb.Tag.ToString() == "home")
                    {
                        cb.Checked = true;
                    }
                    pnlFeeds.Controls.Add(cb);
                    
                }

                Button btn = new Button();
                btn.Text = Properties.Resources.SUBCRIBE_TEXT;
                btn.Height = CFMeasureString.MeasureString(btn, btn.Text, pnlFeeds.ClientRectangle).Height;
                btn.Location = new Point(x, y);
                btn.Width = 120;
                btn.Click += new EventHandler(btn_Click);
                pnlFeeds.Controls.Add(btn);
                //string response = FF.FeedSubscribe(feedId, listID);
            }
            else if (subsWork == Subscription.Unsubscribe)
            {
                Label lblPrompt = new Label();
                lblPrompt.Text = Properties.Resources.UNSUBSCRIBE_PROMPT;
                lblPrompt.Location = new Point(x, y);
                lblPrompt.Width = (pnlFeeds.ClientRectangle.Width - 20);
                lblPrompt.Height = CFMeasureString.MeasureString(lblPrompt, lblPrompt.Text, pnlFeeds.ClientRectangle).Height;
                pnlFeeds.Controls.Add(lblPrompt);
                y += (lblPrompt.Height + 3);

                Button btnYes = new Button();
                btnYes.Text = Properties.Resources.YES;
                btnYes.Width = 45;
                btnYes.Height = CFMeasureString.MeasureString(btnYes, btnYes.Text, btnYes.ClientRectangle).Height;                
                btnYes.Tag = "yes";
                btnYes.Click += new EventHandler(btnYes_Click);
                btnYes.Location = new Point(x, y);
                pnlFeeds.Controls.Add(btnYes);
                x += 50;
                Button btnCancel = new Button();
                btnCancel.Text = Properties.Resources.CANCEL;
                btnCancel.Width = 45;
                btnCancel.Height = CFMeasureString.MeasureString(btnCancel, btnCancel.Text, btnCancel.ClientRectangle).Height;
                btnCancel.Tag = "cancel";
                btnCancel.Click += new EventHandler(btnYes_Click);
                btnCancel.Location = new Point(x, y);
                pnlFeeds.Controls.Add(btnCancel);

            }
        }

        void btnYes_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string tag = btn.Tag.ToString();
            if (tag == "yes")
            {
                string response = null;
                response = FF.FeedUnSubscribe(feedId);
                this.Close();
            }
            else if (tag == "cancel")
            {
                this.Close();
            }
           
        }

        void btn_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            Button btn = (Button)sender;
            string response = null;
            foreach(Control c in pnlFeeds.Controls)
            {
                if((c as CheckBox) != null)
                {
                    CheckBox chk = (CheckBox)c;
                    if (chk.Checked)
                    {
                        string id = chk.Tag.ToString();
                         response = FF.FeedSubscribe(feedId, id);
                    }
                }
            }
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            if (response != null)
            {
              
                if (response == "subscribed")
                {
                    MessageBox.Show(Properties.Resources.SUBSCRIBED_TO_FEED);
                    this.DialogResult = DialogResult.OK;
                }
                else if (response == "requested")
                {
                    MessageBox.Show(Properties.Resources.UNSUBSCRIBED_FROM_FEED);
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    this.DialogResult = DialogResult.Abort;
                }
                this.Close();
            }
                        
        }
    }
}